package com.sakura.mall.order.orderOperateHistory.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.order.orderOperateHistory.domain.entity.OrderOperateHistoryEntity;
import com.sakura.mall.order.orderOperateHistory.domain.request.AddOrderOperateHistoryRequest;
import com.sakura.mall.order.orderOperateHistory.domain.request.UpdateOrderOperateHistoryRequest;
import com.sakura.mall.order.orderOperateHistory.domain.request.OrderOperateHistoryPageRequest;
import com.sakura.mall.order.orderOperateHistory.domain.vo.OrderOperateHistoryVo;
import com.sakura.mall.order.orderOperateHistory.mapper.OrderOperateHistoryMapper;
import com.sakura.mall.order.orderOperateHistory.service.OrderOperateHistoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单操作历史记录 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class OrderOperateHistoryServiceImpl extends ServiceImpl<OrderOperateHistoryMapper, OrderOperateHistoryEntity> implements OrderOperateHistoryService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddOrderOperateHistoryRequest request) {
        // todo 其他校验逻辑
        OrderOperateHistoryEntity orderOperateHistory = request.convert(OrderOperateHistoryEntity.class);
        this.save(orderOperateHistory);
    }

    @Override
    public Page<OrderOperateHistoryVo> queryPage(OrderOperateHistoryPageRequest request) {
        IPage<OrderOperateHistoryEntity> qp = request.toPage();
        LambdaQueryWrapper<OrderOperateHistoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<OrderOperateHistoryVo> ret = qp.convert(entity -> entity.convert(OrderOperateHistoryVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateOrderOperateHistoryRequest request) {
        OrderOperateHistoryEntity entity = request.convert(OrderOperateHistoryEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

